<extend name="Public/base" />

<block name="print">
<style>
    .pdiv{
        padding-top: 1px;
        padding-bottom: 0px;
        padding-left: 10px;
    }
    .padding-xs{
        padding: 20px;
    }
</style>
    <!--<div class="main-title hidden-print margin padding" >-->
        <!--<h2>产品标签打印</h2>-->
    <!--</div>-->
    <div class="row" style="padding-top: 0px;" ng-controller="PrinttagController" ng-init="init()">
        <div style="font-size: 16px;margin-left: -160px;margin-top: -30px;" class="visible-print-block" >
            <!--<div style="font-size: 16px;margin: 0px;" class="" >-->
            <div class="pdiv" style="width:200px;padding-bottom:30px;padding-top: 10px; " ng-repeat="tag in tags">
                <div class="">
                    <span class="">BIN:</span>
                    <span class="">{{tag.BIN}}</span>
                </div>
                <div class="">
                    <span class="">YP:</span>
                    <span class="">{{tag.YP}}</span>
                </div>
                <div class="">
                    <span class=" ">PC:</span>
                    <span class=" ">{{tag.PC}}</span>
                </div>
                <div class="">
                    <span class="">{{tag.TITLE}}</span>
                </div>
            </div>
        </div>
        <div class="col-md-12  hidden-print">
                <div class="panel panel-default">
                    <div class="panel-body">
                      <form class="form-horizontal">
                        <div class="form-item cf">
                            <div class="controls">
                                    <table class="table table-bordered table-hover">
                                        <thead>
                                        <tr>
                                            <th class="col" >商品编码</th>
                                            <th>原包装编码</th>
                                            <th>原包装批次</th>
                                            <th class="col">打印数量</th>
                                            <th class="col">删除</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                            <tr ng-repeat="item in items">
                                                <td class="change">
                                                    <select select2 style="height: 30px; width:200px;" ng-change="changeBIN($index,x)" ng-model="x">
                                                        <option value="">请选择...</option>
                                                        <option ng-repeat="p in product" value="{{p.BIN}},{{p.pname}},{{p.spec}}">{{p.BIN}}({{p.pname}}{{p.spec}})</option>
                                                    </select>
                                                </td>
                                                <td class="change">
                                                    <select select2 style="height: 30px; width:200px;" ng-change="changeYbzBIN($index,item.ybzBIN)" ng-model="item.ybzBIN">
                                                        <option value="">请选择...</option>
                                                        <option ng-repeat="list in item.ybzBINList" value="{{list.BIN}}" >{{list.BIN}}[{{list.pname}}{{list.spec}}]</option>
                                                    </select>
                                                </td>
                                                <td class="change">
                                                    <select select2 style="height: 30px; width:150px;" name="warn_batch[]" ng-model="item.ybzBatch">
                                                        <option value="">请选择...</option>
                                                        <option ng-repeat="ybz in item.ybzBatchList" value="{{ybz.warn_batch}}" >{{ybz.warn_batch}}</option>
                                                    </select>
                                                </td>

                                                <td>
                                                    <input type="number" class="form-control input-2x input" ng-model="item.cnt" />
                                                </td>
                                                <td><button type="button" class="btn btn-warning" ng-click="remove($index)"> x </button></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                    <a ng-click="plus()" class="btn btn-primary">添 加</a>
                                    <a ng-click="print()" class="btn btn-danger">打 印</a>
                            </div>
                            <div class="alert alert-warning alert-dismissible" role="alert">
                              <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                              <strong>提示!</strong> 点击打印后，需要数秒钟时间准备数据，请耐心等待，谢谢配合.
                            </div>
                        </div>
                      </form>
                    </div>
                </div>
            </div>
    </div>


</block>

<block name="script">
    <script type="text/javascript">
        //导航高亮
        highlight_subnav('{:U('PackageManager/printtag')}');
        grErp.controller('PrinttagController',['$scope','$filter','inventoryFactory',function($scope,$filter,inventoryFactory) {
            $scope.items=[{}];
            $scope.tags = [];

            $scope.product = [];

            $scope.init = function(){
              $("#main-content").hide();
              $scope.items = [{},{},{},{},{}];
              //读取正价商品
              inventoryFactory.getproductlist('zj').then(function(d){
                  $scope.product = d;
              });
            }

            $scope.changeBIN = function($index,x){
              var a = x.split(',');
              $scope.items[$index].BIN = x = a[0];
              a.splice(0,1);
              $scope.items[$index].title = a.join();
              inventoryFactory.getybzBINlist(x).then(function(d){
                  $scope.items[$index].ybzBINList = d;
                  // $scope.items[$index].title = d.pname + d.spec;
              });
            }

            $scope.changeYbzBIN = function($index,x){
              inventoryFactory.getbatchlist(x,0).then(function(d){
                  $scope.items[$index].ybzBatchList = d;
              });
            }



            $scope.remove = function($index){
              $scope.items.splice($index,1);
            }
            $scope.plus = function(){
              $scope.items.push({});
            }
            $scope.print = function(){
              //根据数量生成相应条数的数据
              for(var i in $scope.items){
                  var _item = $scope.items[i];
                  var _l = _item.cnt;
                  console.log(_item);
                  while(_l-- > 0){
                      var month=new Date().getMonth()>8?(new Date().getMonth()+1):('0'+(new Date().getMonth()+1));
                      var day=new Date().getDate()>9?new Date().getDate():('0'+(new Date().getDate()))
                      console.log(month);
                      var date=new Date().getFullYear()+month+day;

                      $scope.tags.push({BIN:_item.BIN,YP:$filter('grEncode')(_item.ybzBatch),PC:date,TITLE:_item.title});
                  }
                //  continue;
                  if(i == $scope.items.length - 1){
                    setTimeout(function(){
                      window.print();
                    },2000);
                  }
              }
              //console.log($scope.tags);
            }

          }]);


    </script>
</block>
